package com.kevin.listener;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;

import org.springframework.stereotype.Component;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.kevin.model.config.PropertiesModel;

@WebListener
@Component
public class InitListener implements ServletContextListener {

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        ServletContext context = sce.getServletContext();
    }

    @Override
    public void contextInitialized(ServletContextEvent sce) {
        WebApplicationContextUtils.getRequiredWebApplicationContext(sce.getServletContext()).getAutowireCapableBeanFactory()
                .autowireBean(this);

        // // System.out.println("@WebListener:"+companyShieldService);

        ServletContext context = sce.getServletContext();
        String ctxPath = context.getContextPath();
        context.setAttribute("ctxPath", ctxPath);

        PropertiesModel.registerContext(context); // 注册上下文变量到配置文件Model中
    }
}
